import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  TaskServiceProxy, WorkflowServiceProxy, OutsourcingWorkflowServiceProxy, ChooseSupplierViewModel, OutsourcingServiceProxy, UserServiceProxy, ProcessingDeliveryDto, ProcessingDeliveryViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../shared/organization-unit-tree.component';
import * as _ from 'lodash';

import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
  selector: 'outsourcingQuotationAudit',
  templateUrl: './OutsourcingQuotationAudit.component.html',
  styleUrls: ['./OutsourcingQuotationAudit.component.css']
})
export class OutsourcingQuotationAuditComponent extends AppComponentBase implements OnInit {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('taskdataTable') taskdataTable: Table;
  @ViewChild('dataTable') dataTable: Table;

  @Input() workflowid: any;
  @Output() out = new EventEmitter();

  isLoading = true;
  rows: number;
  canvasWidth: number;
  active = false;
  saving = false;
  _processingDeliveryDto: ProcessingDeliveryDto = new ProcessingDeliveryDto();
  _processingDeliveryViewDto: ProcessingDeliveryViewDto[];
  detailsprimengTableHelper: PrimengTableHelper;

  suppliers: [];
  workflow: any;
  datatablevalues: ChooseSupplierViewModel[];
  ngOnInit(): void {
    console.log(this.workflowid);
    this.initForm();
  }
  constructor(injector: Injector,
    private route: ActivatedRoute,
    private _outsouringServiceProxy: OutsourcingServiceProxy,
    private _OutsourcingWorkFlowServiceProxy: OutsourcingWorkflowServiceProxy,
    private _taskServiceProxy: TaskServiceProxy,
    private _userServiceProxy: UserServiceProxy,
    private _workflowService: WorkflowServiceProxy) {
    super(injector);
    this.detailsprimengTableHelper = new PrimengTableHelper();

  }

  initForm(): void {
    this._OutsourcingWorkFlowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(resultdata => {
      console.log(resultdata)
      this.workflow = resultdata.workflowDto;
      this.suppliers = this.workflow.supplierUnit.split(',');
      this.canvasWidth = this.suppliers.length * 230;
    });
    this._outsouringServiceProxy.getOutsourcingsForChooseSupplier(this.workflowid).subscribe(dto => {
      this.datatablevalues = dto;
      this.rows = dto.length;
      this.active = true;
      this.isLoading = false;
    })
  }
  getSupplierQuotation(suppliername: string, rowindex: number): string {
    let currentD = this.datatablevalues[rowindex];
    let quotation = currentD.quotations.find((element) => (element.supplier === suppliername));
    if (quotation)
    {
      return quotation.quotation;
    }

    return '-';
  }
  audityes(): void {
    this._OutsourcingWorkFlowServiceProxy.outsourcingFlow_execute(this.workflowid, 'yes').subscribe(dto => {
      console.log(dto);
      this.out.emit(dto);
    });
  }

  auditno(): void {
    this._OutsourcingWorkFlowServiceProxy.outsourcingFlow_execute(this.workflowid, 'no').subscribe(dto => {
      console.log(dto);
      this.out.emit(dto);
    });
  }

}
